package cpsobras.web;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import cpsobras.servico.Servico;
import cpsobras.servico.ServicoRN;

@ManagedBean(name="servicoBean")
@SessionScoped
public class ServicoBean implements Serializable {

	private static final long serialVersionUID = 4634100516653213635L;
	private Servico servico = new Servico();
	private List<Servico> lista;
	private List<Servico> pesquisarServico;
	
	public String novo(){
		this.servico = new Servico();
		return "servico";
	}

	public String salvar(){
		ServicoRN servicoRN = new ServicoRN();
		servicoRN.salvar(this.servico);
		FacesContext context = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage("Cadastrado/Alterado com sucesso!");
		context.addMessage(null, facesMessage);

		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("servicoBean");
		this.servico = null;
		return "listar_servico";
	}
	
	public String excluir(){
		ServicoRN servicoRN = new ServicoRN();
		servicoRN.excluir(this.servico);
		this.lista = null;
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("servicoBean");
		return "listar_servico";
	}
		
	public String alterar(){
		return "servico";
	}

	public String voltar(){
		return "listar_servico";
	}

	public List<Servico> getLista() {
		if(this.lista == null){
			ServicoRN servicoRN = new ServicoRN();
			this.lista = servicoRN.listar();
		}
		return this.lista;
	}

	public Servico getServico() {
		return servico;
	}

	public void setServico(Servico servico) {
		this.servico = servico;
	}

	public List<Servico> getPesquisarServico() {
		return pesquisarServico;
	}

	public void setPesquisarServico(List<Servico> pesquisarServico) {
		this.pesquisarServico = pesquisarServico;
	}
}